package com.power200.showcase;

import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import  android.os.Handler;

import com.power200.showcase.presenter.DispatchPresenter;
import com.power200.showcase.presenter.GridViewPresenter;

import org.w3c.dom.Text;

import java.util.logging.LogRecord;

public class MainActivity extends AppCompatActivity {

    private Button calButton ;
    private Button simpleListViewButton;
    private EditText firstEditText;
    private EditText secondEditText;
    private CheckBox checkBox;
    private RadioGroup rg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        firstEditText = (EditText) findViewById(R.id.first);
        secondEditText = (EditText) findViewById(R.id.second);

        calButton = (Button) findViewById(R.id.calButton);
        calButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this ,ResultActivity.class);
                Bundle bundle = new Bundle();

                String firstStr = firstEditText.getText().toString();
                String secondString = secondEditText.getText().toString();
                bundle.putString(getResources().getString(R.string.first),firstStr);
                bundle.putString(getResources().getString(R.string.second),secondString);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        simpleListViewButton = (Button) findViewById(R.id.simpleListViewButton);
        simpleListViewButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SimpleListActivity.class);
                startActivity(intent);
            }
        });

        Button comboListViewButton = (Button) findViewById(R.id.comboListViewButton);
        comboListViewButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,ComboListViewActivity.class);
                startActivity(intent);
            }
        });

        // checkbox
        checkBox = (CheckBox) findViewById(R.id.cb);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(MainActivity.this,isChecked?"Love":"Hate",Toast.LENGTH_LONG).show();
            }
        });

        // radiobutton
        rg = (RadioGroup) findViewById(R.id.rg);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton rb = (RadioButton) findViewById(checkedId);
                TextView textView = (TextView) findViewById(R.id.sexDis);
                textView.setText("Sex: " + rb.getText());
            }
        });

        // progress bar
        final ProgressBar pg = (ProgressBar)findViewById(R.id.pg);
        final TextView pgDisplay = (TextView)findViewById(R.id.pgDisplay);
        final Handler pgHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                pg.setProgress(msg.arg1);
                pg.setSecondaryProgress(msg.arg2);
                pgDisplay.setText(msg.arg1+"%");
            }
        };

        new Thread(){
            private  int progress = 0;
            @Override
            public void run() {
                while (true) {
                    Message message = pgHandler.obtainMessage();
                    progress ++;
                    if(progress == 100) {
                        progress = 0;
                    }
                    message.arg1 = progress;
                    message.arg2 = (int) Math.random() * 100;

                    pgHandler.sendMessage(message);
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        //broadcast
        Button register = (Button) findViewById(R.id.register);
        Button unregister = (Button) findViewById(R.id.unregister);

        final WifiManager wifiManager = (WifiManager) MainActivity.this.getSystemService(WIFI_SERVICE);
        final Button wifi = (Button) findViewById(R.id.wifi);
        wifi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wifi.setEnabled(false);
                int wifiState = wifiManager.getWifiState();
                switch (wifiState) {
                    case WifiManager.WIFI_STATE_DISABLED:
                        wifiManager.setWifiEnabled(true);
                        break;
                    case WifiManager.WIFI_STATE_ENABLED:
                        wifiManager.setWifiEnabled(false);
                        break;
                    case WifiManager.WIFI_STATE_DISABLING:
                    case WifiManager.WIFI_STATE_ENABLING:
                    case WifiManager.WIFI_STATE_UNKNOWN:
                        break;
                    default:
                        break;
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
                wifi.setEnabled(true);
            }
        });
        final Handler wifiHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                int wifiState = wifiManager.getWifiState();
                switch (wifiState) {
                    case WifiManager.WIFI_STATE_DISABLED:
                        wifi.setText("-");
                        break;
                    case WifiManager.WIFI_STATE_DISABLING:
                        wifi.setText("--");
                        break;
                    case WifiManager.WIFI_STATE_ENABLED:
                        wifi.setText("+");
                        break;
                    case WifiManager.WIFI_STATE_ENABLING:
                        wifi.setText("+-");
                        break;
                    case WifiManager.WIFI_STATE_UNKNOWN:
                        wifi.setText("?");
                        break;
                    default:
                        wifi.setText("?");
                        break;
                }
                super.handleMessage(msg);
            }
        };
        new Thread(){
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    wifiHandler.sendMessage(new Message());
                }
            }
        }.start();

        new GridViewPresenter().init(this);
        new DispatchPresenter().init(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
